package com.ymatou.iapi.sellerproduct.testcase;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.ModifyActivityProductSortV3Call;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

public class Ts_ModifyActivityProductSortV3 {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static ModifyActivityProductSortV3Call modifyActivityProductSortV3Call= new ModifyActivityProductSortV3Call();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播商品排序");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(modifyActivityProductSortV3Call.getOpurl());
	}
	
	@After	
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ModifyActivityProductSortV3() throws Exception {
		Logger.start(true,"直播商品排序");
		try{
			int prodCount = 10;
			
			//1 add new product
			List<String> productIds = new ArrayList<String>();
			for(int i=0;i<prodCount;i++){
				JSONObject productReqBean = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);		
				String productId =  productReqBean.getJSONObject("Data").getString("ProductID");
				productIds.add(productId);
			}
			
			
			//2 create new live in process
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//3 add product into live 正在进行的直播
			SellerProductCallService.addProductsInLive(sellerId, liveId, productIds);
			
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/ModifyActivityProductSortV3Call/modifyactivityproductsortv3calltpl_001.json");
			Map<String, Object> updateMap = MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId);
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
			for(int i=0;i<prodCount;i++){
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("ProductId", productIds.get(i));
				jsonObject.put("Index", prodCount-i-1);
				addProductBean.getJSONArray("ActivityProductSorts").put(i, jsonObject);
				
			}
			
			JSONObject ret = modifyActivityProductSortV3Call.callAndGetReturnData(addProductBean);
			Logger.verifyEquals(200, ret.getInt("Code"), "报文返回码");
			
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				YMTDateUtil.waitTime(20);
				YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
				LiveProductsWapper liveProductsWapper = new LiveProductsWapper();
				
				productIds.forEach(prod->{
					
					try {
						String sqlSortVal = ymtProductsInLiveWapper.selectInLiveByProductId(prod).get(0).get("dReadyPutawayProductSort").toString();
						String mongoSortVal = liveProductsWapper.getByProductId(prod).get("sort").toString();
						AssertService.assertResultEqual(MapUtil.hashMap("sort", sqlSortVal),MapUtil.hashMap("sort", mongoSortVal),"Mongo-LiveProducts 直播排序检查 id:"+prod);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				});
			}
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
		
	}
}
